package com.castle.fortress.admin.cms.fragment;

import cn.hutool.core.util.StrUtil;
import com.castle.fortress.admin.cms.dto.CmsChannelDto;
import com.castle.fortress.admin.cms.dto.CmsModelDto;
import com.castle.fortress.admin.cms.service.CmsArticleService;
import com.castle.fortress.admin.cms.service.CmsChannelService;
import com.castle.fortress.admin.cms.service.CmsModelService;
import com.castle.fortress.admin.utils.SpringUtils;
import org.beetl.core.tag.GeneralVarTagBinding;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * cms 栏目文章列表
 * @author castle
 */
public class CmsArticleListTag extends GeneralVarTagBinding {

	@Override
	public void render() {
		CmsArticleService cmsArticleService = SpringUtils.getBean(CmsArticleService.class);
		CmsModelService cmsModelService =  SpringUtils.getBean(CmsModelService.class);
		List<Map<String,Object>> list =new ArrayList<>();
		Map<String,Object> map = new HashMap<>();
		Map<String,Object> channelMap = new HashMap<>();
		//指定栏目编码 多个栏目用逗号隔开
		if(containHtmlAttribute("code")){
			channelMap.put("code",getAttributeValue("code"));
			channelMap.put("level","self");
		}
		//筛选的栏目级别,默认级别是本级 level：self(本级)、all(本级及子集)、child(子集)
		if(containHtmlAttribute("level")){
			if("self".equals(getAttributeValue("level").toString())||"all".equals(getAttributeValue("level").toString())||"child".equals(getAttributeValue("level").toString())){
				channelMap.put("level",getAttributeValue("level"));
			}
		}
		try {
			//模型筛选
			if(containHtmlAttribute("model")&&StrUtil.isNotEmpty(getAttributeValue("model").toString())&&!"article".equals(getAttributeValue("model").toString())){
				//查询栏目模型
				CmsModelDto m=cmsModelService.getByCode(getAttributeValue("model").toString());
				//扩展模型
				if(m!=null){
					map.put("modelFlag",m.getCode());
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		//是否要栏目筛选
		if(channelMap.get("code")!=null && StrUtil.isNotEmpty(channelMap.get("code").toString())){
			map.put("channelFlag",true);
			List<Long> ids=new ArrayList<>();
			CmsChannelService channelService = SpringUtils.getBean(CmsChannelService.class);
			List<CmsChannelDto> channels=null;
			//只查询本级栏目
			if("self".equals(channelMap.get("level").toString())){
				channels = channelService.cmsTagListCmsChannel(channelMap);
				for(CmsChannelDto dto:channels){
					ids.add(dto.getId());
				}
			}else{
				String codes = channelMap.get("code").toString();
				channels = channelService.cmsTagListCmsChannel(new HashMap<>());
				List<CmsChannelDto> parents=new ArrayList<>();
				List<CmsChannelDto> sons=new ArrayList<>();
				for(CmsChannelDto dto:channels){
					if(codes.indexOf(dto.getCode())!=-1){
						parents.add(dto);
					}
				}
				if(!parents.isEmpty()){
					for(CmsChannelDto dto:channels){
						if(StrUtil.isNotEmpty(dto.getParents())){
							for(CmsChannelDto p:parents){
								if(dto.getParents().indexOf(p.getId().toString())!=-1){
									sons.add(dto);
									break;
								}
							}
						}
					}
				}
				for(CmsChannelDto dto:sons){
					ids.add(dto.getId());
				}
				if("all".equals(channelMap.get("level").toString())){
					for(CmsChannelDto dto:parents){
						ids.add(dto.getId());
					}
				}
			}
			if(ids.isEmpty()){
				this.binds(list);
				this.doBodyRender();
				return;
			}
			map.put("channels",ids);
		}else{
			map.put("channelFlag",false);
		}

		//排序字段 orderby：self(本级)、all(本级及子集)、child(子集) | 排序方式 orderway：desc asc
		if(containHtmlAttribute("orderby")  && containHtmlAttribute("orderway")){
			if(("id".equals(getAttributeValue("orderby").toString())
			|| "pubdate".equals(getAttributeValue("orderby").toString())
			|| "sort".equals(getAttributeValue("orderby").toString())
			|| "click".equals(getAttributeValue("orderby").toString())
			)&&("asc".equals(getAttributeValue("orderway").toString())||"desc".equals(getAttributeValue("orderway").toString()))){
				map.put("orderby",getAttributeValue("orderby"));
				map.put("orderway",getAttributeValue("orderway"));
			}
		}
		//标题长度限制
		if(containHtmlAttribute("titlelen")&&StrUtil.isNotEmpty(getAttributeValue("titlelen").toString())){
			try {
				Integer titlelen = Integer.valueOf(getAttributeValue("titlelen").toString());
				if(titlelen>0){
					map.put("titlelen",titlelen);
				}
			} catch (NumberFormatException e) {
				e.printStackTrace();
			}
		}
		//内容摘要长度
		if(containHtmlAttribute("infolen")&&StrUtil.isNotEmpty(getAttributeValue("infolen").toString())){
			try {
				Integer infolen = Integer.valueOf(getAttributeValue("infolen").toString());
				if(infolen>0){
					map.put("infolen",infolen);
				}
			} catch (NumberFormatException e) {
				e.printStackTrace();
			}
		}
		//文档属性(系统预设标签) 多个栏目用逗号隔开
		if(containHtmlAttribute("flag")){
			map.put("flag",getAttributeValue("flag"));
		}
		//过滤文章数目
		if(containHtmlAttribute("row")&&StrUtil.isNotEmpty(getAttributeValue("row").toString())){
			try {
				Integer rows = Integer.valueOf(getAttributeValue("row").toString());
				map.put("row",rows);
			} catch (NumberFormatException e) {
				e.printStackTrace();
			}
		}
		//数目过滤起始
		if(containHtmlAttribute("start")&&StrUtil.isNotEmpty(getAttributeValue("start").toString())){
			try {
				Integer start = Integer.valueOf(getAttributeValue("start").toString());
				map.put("start",start);
			} catch (NumberFormatException e) {
				e.printStackTrace();
			}
		}
		Integer start=0;
		if(map.get("start")!=null&&StrUtil.isNotEmpty(map.get("start").toString())){
			start = Integer.valueOf(map.get("start").toString());
		}
		//分页显示
		if(containHtmlAttribute("pagesize")&&containHtmlAttribute("pageindex")
				&&StrUtil.isNotEmpty(getAttributeValue("pagesize").toString())
				&&StrUtil.isNotEmpty(getAttributeValue("pageindex").toString())){
			try {
				Integer pagesize = Integer.valueOf(getAttributeValue("pagesize").toString());
				Integer pageindex = Integer.valueOf(getAttributeValue("pageindex").toString());
				if(pagesize>0&&pageindex>=1){

					Integer limitstart = (pageindex-1)*pagesize+start;
					map.put("limitstart",limitstart);
					map.put("limitrow",pagesize);
				}

			} catch (NumberFormatException e) {
				e.printStackTrace();
			}
		}
		list = cmsArticleService.cmsTagChannelCmsArticle(map);
		if(map.get("limitstart")!=null && map.get("limitrow")!=null){
			Integer total = cmsArticleService.cmsTagCountChannelCmsArticle(map);
			if(total>=start){
				total=total-start;
			}else{
				total=0;
			}
			Integer pagesize = Integer.parseInt(map.get("limitrow").toString());
			//总页数
			Integer totalpage = total%pagesize==0?(total/pagesize):((total/pagesize)+1);
			//绑定对象 列表、总页数、总记录数
			this.binds(list,totalpage,total);
		}else{
			this.binds(list);
		}
		this.doBodyRender();
	}
}
